package com.shop.api;

import com.shop.ex.ShoppingException;
import com.shop.feignClient.UserClient;
import com.shop.merchant.Interceptor.GetMerchantInfoInterceptor;
import com.shop.merchant.po.Merchant;
import com.shop.merchant.po.dto.MerchantDto;
import com.shop.merchant.service.MerchantService;
import com.shop.po.R;
import com.shop.po.dto.OrderDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 商家信息 前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-23
 */
@RestController
@RequestMapping("/merchant")
@CrossOrigin
public class MerchantController {

    @Autowired
    private MerchantService merchantService;


    @PostMapping("/edit")
    public R editUserInfo(@RequestBody(required = false) MerchantDto merchantDto,
                          @RequestParam(required = false) String password){
        if (merchantDto==null && password==null){
            ShoppingException.ThrowShoppingException("操作失败，请参数参数");
        }
        merchantService.editMerchantInfo(merchantDto,password);
        return R.ok("成功编辑用户信息");
    }



    @GetMapping("/getInfo")
    public R getUserInfo(){
        MerchantDto merchantDto= merchantService.getMerchantInfo();
        return R.ok("成功获取用户信息").data("merchant",merchantDto);
    }

    @PostMapping("/register")
    public R register(@RequestBody MerchantDto merchantDto,
                      @RequestParam("password") String password,
                      @RequestParam("code") String code) {
        merchantService.register(merchantDto,password,code);
        return R.ok("注册成功");
    }

    @PostMapping("/loginByPassword/{account}/{password}")
    public R loginByPassword(@PathVariable String account, @PathVariable String password) {
        String token= merchantService.loginByPassword(account, password);
        return R.ok("登录成功").data("token",token);
    }

    @PostMapping("/loginByCode/{phone}/{code}")
    public R loginByCode(@PathVariable String phone, @PathVariable String code) {
        String token= merchantService.loginByCode(phone, code);
        return R.ok("登录成功").data("token",token);
    }

    /**
     * 发送验证码
     * @param phone
     * @param type:验证码用途
     */
    @GetMapping("/sendMessage/{phone}/{type}")
    public R sendMessage(@PathVariable("phone") String phone, @PathVariable("type") String type){
        return merchantService.sendMessage(phone,type);
    }

    /**
     * 上传文件（头像）
     * @param file
     * @return
     */
    @PostMapping("/uploadFile/{type}")
    public R uploadFile(@RequestParam("file") MultipartFile file, @PathVariable("type") String type){
        return merchantService.uploadFile(file,type);
    }


    @Autowired
    private UserClient userClient;

    //通过Feign调用
    @GetMapping("/order/merchantGetOrderInfo")
    public R merchantGetOrderInfo(
            @RequestParam(value = "state",required = false) String state,
            @RequestParam(value = "currentPage",required = true) Long page,
            @RequestParam(value = "size",required = true) Long size
    ){
        Merchant merchant = GetMerchantInfoInterceptor.getMerchant();
        if (merchant==null){
            ShoppingException.ThrowShoppingException("请先登录");
        }
        //获取列表数据
        List<OrderDetailInfo> list = userClient.merchantGetOrderInfo(merchant.getId(), state, page, size);
        //获取列表总数
        Long total = userClient.merchantGetTotal(merchant.getId(), state);
        return R.ok().data("list",list).data("total",total);
    }

    //修改订单状态
    @PostMapping("/changState/{state}/{orderDetailsId}")
    public R changState(@PathVariable("state")String state,
                        @PathVariable("orderDetailsId") String orderDetailsId){
        userClient.changState(state,orderDetailsId);
        return R.ok();
    }




}
